﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Home
{
	public partial class fMain : Form
	{
		public fMain()
		{
			InitializeComponent();
			tabMain.SelectedIndex = 1;
			home = new Home();
			var r1 = new Route(new DateTime(2015, 1, 5), new DateTime(2015, 1, 16), "Stolberg");
			r1.SetBudget(300, 200, 265);
			r1.AddEntry(2015, 1, 6, 7, 0, "Vonat", 142, true);
			r1.AddEntry(2015, 1, 6, 16, 0, "Kaja", 8.22f, false);
			r1.AddEntry(2015, 1, 7, 9, 0, "Taxi", 8.5f, true);
			r1.AddEntry(2015, 1, 8, 9, 0, "Taxi", 8.5f, true);
			r1.AddEntry(2015, 1, 9, 10, 0, "Taxi", 8.5f, true);
			r1.AddEntry(2015, 1, 9, 16, 0, "Kaja", 6.01f, false);
			r1.AddEntry(2015, 1, 12, 8, 30, "Taxi", 8.5f, true);
			r1.AddEntry(2015, 1, 12, 18, 0, "Kaja", 7.83f, false);
			r1.AddEntry(2015, 1, 14, 9, 0, "Taxi", 8.5f, true);
			r1.AddEntry(2015, 1, 14, 19, 0, "Kaja", 9.00f, false);
			r1.AddEntry(2015, 1, 15, 9, 0, "Taxi", 8.5f, true);
			r1.AddEntry(2015, 1, 16, 9, 0, "Taxi", 8.5f, true);
			r1.AddEntry(2015, 1, 16, 16, 0, "Vonat", 13.9f, true);
			home.Routes.Add(r1);

			var r = new Route(new DateTime(2015, 2, 2), new DateTime(2015, 2, 28), "Stolberg");
			r.SetBudget(300, 0, 100);
			home.Routes.Add(r);
			r.AddLength("bud->sto", 1270);
			r.AddLength("daily(19*7)", 7 * 19);
			r.AddLength("sto->slo", 1031);
			r.AddLength("slo->sto", 1031);
			r.AddLength("slo->bud", 400);
			r.AddLength("sto->bud", 1270);
			r.AddLength("sto->bud", 1270);
			
			r.AddFuel(2015, 2, 2, 19, 30, 65, 1.229f);

			r.AddEntry(2015, 2, 4, 18, 0, "Kaja", 7.5f, false);
			r.AddEntry(2015, 2, 5, 12, 0, "Ebéd", 4.3f, false);
			SaveHome();

			Console.WriteLine("Routes: " + home.Routes.Count);
			Console.WriteLine("Total distance: " + r.TotalLength);
			Console.WriteLine("Fuel covered:   " + r.TankCover);
			Console.WriteLine("Fuel price  :   " + r.FuelPrice);
			Console.WriteLine("Price covered:  " + r.TankCover * r.FuelPrice);
			Console.WriteLine("Fuel used:      " + r.TotalTanked);

			Draw();
		}

		void Draw()
		{
			FillRoutes();
		}

		float balance;
		void MakeRouteControl(Route r, int N)
		{
			Panel P = new Panel();
			P.Cursor = Cursors.Hand;
			P.Location = new Point(0, N * 55);
			P.Size = new Size(tRoutes.Width, 55);
			P.BackColor = N % 2 == 0 ? Color.White : Color.Wheat;
			P.Click += (x, y) =>
			{
				if (new fRoute(r).ShowDialog() == DialogResult.OK)
					Draw();
			};
			AddLabel(P, "lName", 0, 3, r.ToString());

			//Daily
			AddLabel(P, "lDaily", 0, 250, "Daily: " + r.Daily);
			AddLabel(P, "lPush", 2, 250, "Push: " + r.Pushed);
			float pushed = r.Pushed;

			//Own
			AddLabel(P, "lOwn", 0, 320, "Own: " + r.OwnRemain);
			AddLabel(P, "lSpend", 1, 320, "Spent: " + r.Spent);
			AddLabel(P, "lRem", 2, 320, r.OwnRemain.ToString("0.00")).ForeColor = r.OwnRemain > 0 ? Color.Lime : Color.Red;

			//Comp
			AddLabel(P, "lComp", 0, 400, "Comp: " + r.Company);
			AddLabel(P, "lSpendC", 1, 400, "Spent: " + r.SpentComp);
			AddLabel(P, "lRemC", 2, 400, r.CompRemain.ToString("0.00")).ForeColor = r.CompRemain> 0 ? Color.Lime : Color.Red;

			AddLabel(P, "lFuel", 0, 480, "Fuel: " + r.TankCover);
			AddLabel(P, "lCov", 1, 480, "Used: " + r.TotalTanked);
			AddLabel(P, "lFuelRem", 2, 480, r.TankLeft.ToString("0.00")).ForeColor = r.TankLeft > 0 ? Color.Lime : Color.Red;
			{
				Label l = AddLabel(P, "lCar", 0, 570, "Car: " + r.Car.ToString("0.00"));
				if (pushed > 0)
					l.ForeColor = Color.Lime;
			}
			float compback = r.CompRemain;
			if (compback >= 0)
			{
				Label l = AddLabel(P, "lBack", 0, 630, "B: " + compback.ToString("0.00"));
				l.ForeColor = Color.Red;
			}
			else
			{
				Label l = AddLabel(P, "lBack", 1, 630, "C: " + (-compback).ToString("0.00"));
				l.ForeColor = Color.Lime;
			}
			float chg = pushed + r.OwnRemain + r.TankCover + r.Car;
			chg = r.Daily + r.TankCover + r.Car - r.Spent;
			Console.WriteLine(balance + chg);
			AddLabel(P, "lCh", 0, 750, "Change: " + chg.ToString("0.00"));
			tRoutes.Controls.Add(P);
			balance += chg;
			AddLabel(P, "lCh", 0, 840, "Balance: " + balance.ToString("0.00"));
		}

		Label AddLabel(Panel p, string name, int row, int X, string txt)
		{
			Label l = new Label();
			l.AutoSize = true;
			l.Location = new Point(X, 3 + row * 18);
			l.Name = name;
			l.Text = txt;
			p.Controls.Add(l);
			return l;
		}
		void FillRoutes()
		{
			balance = home.StartAmount;
			tRoutes.Controls.Clear();
			GC.Collect();
			for (int i = 0; i < home.Routes.Count; i++)
			{
				MakeRouteControl(home.Routes[i], i);
			}
		}
	}
}